/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: kfa_mux.c
 *
 * Purpose: implementation a muxer to mux kfa(key frame animation) file
 *
 * Developer:
 *   wen.gu , 2022-03-04
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "kfa_mux.h"

#include "kfa_io.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
KfaMux::KfaMux()
{
    /** todo something */
}

KfaMux::~KfaMux()
{
    printf("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
    if (out_fp_)
    {
        printf("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
        kfa_write_value(out_fp_, (uint32_t)0); /** set tail */
        printf("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
        fflush(out_fp_);
        printf("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
        fclose(out_fp_);
    }
    printf("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
}

bool KfaMux::setOutputFile(const std::string& file_name)
{
    if (out_fp_)
    {
        return false;
    }

    out_fp_ = fopen(file_name.c_str(), "wb");

    return out_fp_ ? true : false;
} 

bool KfaMux::setHeader(const kfa_header& header)
{
    size_t ret = kfa_write_bin(out_fp_, &header, sizeof(header));

    if (ret != sizeof(header))
    {
        return false;
    }

    return true;
}

bool KfaMux::write(const uint8_t* data, uint32_t size)
{
    size_t ret = 0;
#if 0
    ret = kfa_write_value(out_fp_, in_pkt.frame_count);
    if (ret != 1)
    {
        return false;
    }
 

    ret = kfa_write_value(out_fp_, in_pkt.raw_size);
    if (ret != 1)
    {
        return false;
    }
#endif 

    ret = kfa_write_value(out_fp_, size);
    if (ret != 1)
    {
        return false;
    }

    ret = kfa_write_bin(out_fp_, data, size);

    return ret == size;
}